home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageio.lib / dev / examples / readfile / main.c next >
Encoding:
C/C++ Source or Header  |  2000-08-09  |  4.6 KB  |  197 lines

  1. /* This example use of imageio.library loads the image file specified
  2.         on the command line using the filename tag and views it halved in size
  3.         in a window using guigfx.library.
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <dos/dos.h>
  9. #include <exec/memory.h>
  10. #include <exec/types.h>
  11.  
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/intuition_protos.h>
  15.  
  16. #include <pragmas/dos_pragmas.h>
  17. #include <pragmas/exec_pragmas.h>
  18. #include <pragmas/intuition_pragmas.h>
  19.  
  20. #include <imageio/imageio.h>
  21. #include <imageio/imageio_protos.h>
  22. #include <imageio/imageio_pragmas.h>
  23.  
  24. #include <guigfx/guigfx.h>
  25. #include <pragmas/guigfx_pragmas.h>
  26. #include <guigfx/guigfx_protos.h>
  27.  
  28. /* Function prototypes */
  29. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  30. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y );
  31.  
  32. extern struct Library *DOSBase;
  33. struct Library *ImageIOBase, *IntuitionBase;
  34.  
  35. void main( int argc, char **argv )
  36. {
  37.     if ( argv[1] != NULL )
  38.     {
  39.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  40.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  41.         if ( IntuitionBase && ImageIOBase )
  42.         {
  43.             struct ImageHandle *ih;
  44.             ULONG err;
  45.  
  46.             err = AllocImage( &ih,
  47.                 IMG_SrcFilename, argv[1],
  48.                 TAG_DONE );
  49.             if ( !err )
  50.             {
  51.                 ULONG num = 1, denom = 2;
  52.                 ULONG x, y, bpp, rs;
  53.                 UBYTE *buffer, cs, it;
  54.  
  55.                 err = GetImageAttrs( ih,
  56.                     IMG_ImageType, &it,
  57.                     TAG_DONE );
  58.                 if ( !err )
  59.                 {
  60.                     printf("Image type=%d\n",it);
  61.                 }
  62.  
  63.                 err = GetImageAttrs( ih,
  64.                     IMG_Width, &x,
  65.                     IMG_Height,&y,
  66.                     IMG_BytesPerPixel, &bpp,
  67.                     IMG_ColourSpace, &cs,
  68.                     IMG_RowSize, &rs,
  69.                     IMG_TestScaleNum, num,
  70.                     IMG_TestScaleDenom, denom,
  71.                     TAG_DONE );
  72.                 if ( !err )
  73.                 {
  74.                     printf( "width=%ld, height=%ld\n", x, y );
  75.                     printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  76.                     printf( "row size=%ld\n", rs );
  77.  
  78.                     err = ReadImage( ih,
  79.                         IMG_ScaleNum, num,
  80.                         IMG_ScaleDenom, denom,
  81.                         IMG_ImageBuffer, &buffer,
  82.                         IMG_ProgressHook, progressFunc,
  83.                         TAG_DONE );
  84.                     if ( !err )
  85.                     {
  86.                         /* Display image  - uncomment the line you need */
  87.                         DisplayGuiGfx( buffer, cs, rs, x, y );
  88.                     }
  89.                     else printf( "read image error:%d\n", err );
  90.                 }
  91.                 else printf( "get image attrs error:%d\n", err );
  92.  
  93.                 FreeImage( ih );
  94.             }
  95.             else printf( "alloc image error:%d\n", err );
  96.         }
  97.  
  98.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  99.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  100.     }
  101.     else printf( "no file specified\n" );
  102. }
  103.  
  104. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  105. {
  106.     static int prevpercent = 0;
  107.  
  108.     int percent = ( curr * 100 ) / lines;
  109.  
  110.     if ( prevpercent != percent )
  111.     {
  112.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  113.     }
  114.  
  115.     prevpercent = percent;
  116.  
  117.     return NULL;
  118. }
  119.  
  120. void DisplayGuiGfx( UBYTE *buffer, UBYTE cs, ULONG rs, ULONG x, ULONG y )
  121. {
  122.     struct Library *GuiGFXBase;
  123.  
  124.     GuiGFXBase = OpenLibrary( "guigfx.library", NULL );
  125.     if ( GuiGFXBase )
  126.     {
  127.         struct Window *win;
  128.         APTR dh, pi;
  129.         UBYTE *argb;
  130.  
  131.         win = OpenWindowTags( NULL,
  132.             WA_Title, "Proof",
  133.             WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  134.                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  135.                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  136.             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  137.                 IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  138.             WA_Left, 16,
  139.             WA_Top, 16,
  140.             WA_Width, x,
  141.             WA_Height, y,
  142.             TAG_DONE );
  143.  
  144.         if ( win != NULL )
  145.         {
  146.             dh = ObtainDrawHandle( NULL, win->RPort, win->WScreen->ViewPort.ColorMap, TAG_DONE );
  147.  
  148.             if ( dh )
  149.             {
  150.                 argb = AllocVec( x * y * 4, MEMF_PUBLIC | MEMF_CLEAR );
  151.  
  152.                 if ( argb )
  153.                 {
  154.                     int i, count = 0;
  155.                     char **dest;
  156.  
  157.                     /* Convert an RGB buffer to an ARGB buffer setting A to 0.*/
  158.                     dest = (char **)argb;
  159.  
  160.                     for ( i = 0; i < x * y * 3; i += 3 )
  161.                     {
  162.                         dest[count++] = (char *)( ( (ULONG) * (char**)&buffer[i] ) >> 8 );
  163.                     }
  164.  
  165.                     pi = MakePicture( argb, x, y, GGFX_PixelFormat, PIXFMT_0RGB_32, TAG_DONE );
  166.  
  167.                     if ( pi )
  168.                     {
  169.                         struct Message *msg;
  170.  
  171.                         DrawPicture( dh, pi, 0, 0, NULL );
  172.  
  173.                         Wait( 1L << win->UserPort->mp_SigBit );
  174.  
  175.                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  176.  
  177.                         DeletePicture( pi );
  178.                     }
  179.                     else printf( "failed to create picture\n" );
  180.  
  181.                     FreeVec( argb );
  182.                 }
  183.                 else printf( "failed to allocate argb buffer\n" );
  184.  
  185.                 ReleaseDrawHandle( dh );
  186.             }
  187.             else printf( "failed to get drawhandle\n" );
  188.  
  189.             CloseWindow( win );
  190.         }
  191.         else printf( "failed to open window\n" );
  192.     }
  193.     else printf( "failed to open guigfx.library\n" );
  194.  
  195.     if ( GuiGFXBase ) CloseLibrary( GuiGFXBase );
  196. }
  197.